package strategic_monitor

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/authorize"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/business_opportunity"
	opptyPb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	"strings"
)

type strategicMonitorService struct {
	strategicMonitorRepo business_opportunity.StrategicMonitorRepositoryIface
}

func NewStrategicMonitorService(
	strategicMonitorRepo business_opportunity.StrategicMonitorRepositoryIface,
) StrategicMonitorServiceIface {
	return &strategicMonitorService{
		strategicMonitorRepo: strategicMonitorRepo,
	}
}

func (s *strategicMonitorService) GetStrategicBidList(ctx context.Context, request *dto.GetStrategicBidListRequest) (*dto.GetStrategicBidResponse, error) {
	if request == nil {
		return nil, errors.New("GetStrategicBidList 参数错误")
	}

	data, err := s.strategicMonitorRepo.GetStrategicBidList(ctx, &pb.GetStrategicBidListRequest{
		Keyword:     request.Keyword,
		BidType:     request.BidType,
		AuditStatus: request.AuditStatus,
		Page:        request.Page,
		PageSize:    request.PageSize,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("GetStrategicBidList 获取数据失败")
	}
	resp := &dto.GetStrategicBidResponse{
		Total: data.Total,
		List:  nil,
	}
	for _, item := range data.List {
		resp.List = append(resp.List, &dto.StrategicBidItem{
			Id:          item.Id,
			BidType:     item.BidType,
			CompanyName: item.CompanyName,
			Title:       item.Title,
			PublishDate: item.PublishTime,
			AuditStatus: item.AuditStatus,
		})
	}
	return resp, nil
}

func (s *strategicMonitorService) GetStrategicBidDetail(ctx context.Context, request *dto.GetStrategicBidDetailRequest) (*dto.GetStrategicBidDetailResponse, error) {
	if request == nil {
		return nil, errors.New("GetStrategicBidDetail 参数错误")
	}
	data, err := s.strategicMonitorRepo.GetStrategicBidDetail(ctx, &pb.GetStrategicBidDetailRequest{
		Id:      request.Id,
		BidType: request.BidType,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("GetStrategicBidDetail 获取数据失败")
	}
	resp := &dto.GetStrategicBidDetailResponse{
		Id:          data.Id,
		BidType:     data.BidType,
		CompanyName: data.CompanyName,
		PublishDate: data.PublishTime,
		ProcureType: data.ProcureType,
		StartYear:   data.StartYear,
		EndYear:     data.EndYear,
		Content:     data.Content,
		SourceUrl:   data.SourceUrl,
		Industries:  make([]*dto.IndustryMiniItem, 0),
	}
	if len(data.Industries) != 0 {
		for _, item := range data.Industries {
			resp.Industries = append(resp.Industries, &dto.IndustryMiniItem{
				Id:           item.Id,
				IndustryName: item.IndustryName,
			})
		}
	}
	return resp, nil
}

func (s *strategicMonitorService) AuditStrategicBid(ctx context.Context, request *dto.AuditStrategicBidRequest) (*dto.AuditStrategicBidResponse, error) {
	if request == nil {
		return nil, errors.New("AuditStrategicBid 参数错误")
	}

	data, err := s.strategicMonitorRepo.AuditStrategicBid(ctx, &pb.AuditStrategicBidRequest{
		Id:              request.Id,
		BidType:         request.BidType,
		CompanyName:     request.CompanyName,
		PublishTime:     request.PublishDate,
		ProcureType:     request.ProcureType,
		StartYear:       request.StartYear,
		EndYear:         request.EndYear,
		IndustryNameArr: request.IndustryNameArr,
		AuditUser:       authorize.GetLoginZhanTuUserName(ctx),
	})

	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("审核失败")
	}
	return &dto.AuditStrategicBidResponse{IsSuccess: data.IsSuccess}, nil
}

func (s *strategicMonitorService) GetBidCompanyList(ctx context.Context, request *dto.GetBidCompanyListRequest) (*dto.GetBidCompanyListResponse, error) {
	if request == nil {
		return nil, errors.New("GetBidCompanyList 参数错误")
	}

	data, err := s.strategicMonitorRepo.GetBidCompanyList(ctx, &pb.GetBidCompanyListRequest{
		Keyword:     request.Keyword,
		AuditStatus: request.AuditStatus,
		Page:        request.Page,
		PageSize:    request.PageSize,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("GetBidCompanyList 获取数据失败")
	}

	resp := &dto.GetBidCompanyListResponse{
		Total: data.Total,
		List:  make([]*dto.BidCompanyItem, 0),
	}
	for _, item := range data.List {
		resp.List = append(resp.List, &dto.BidCompanyItem{
			Id:             item.Id,
			CompanyName:    item.CompanyName,
			OrganizingCode: item.OrganizingCode,
			Province:       item.Province,
			City:           item.City,
			AuditStatus:    item.AuditStatus,
			AuditTime:      item.AuditTime,
			AuditBy:        item.AuditBy,
			BidCount:       item.BidCount,
			CooperateCount: item.CooperateCount,
			CreatedBy:      item.CreatedBy,
			CreatedOn:      item.CreatedOn,
		})
	}

	return resp, nil
}

func (s *strategicMonitorService) GetBidCompanyDetail(ctx context.Context, request *dto.GetBidCompanyDetailRequest) (*dto.BidCompanyItem, error) {
	if request == nil {
		return nil, errors.New("GetBidCompanyDetail 参数错误")
	}

	data, err := s.strategicMonitorRepo.GetBidCompanyDetail(ctx, &pb.GetBidCompanyDetailRequest{Id: request.Id})
	if err != nil {
		return nil, err
	}
	if data == nil || data.Detail == nil {
		return nil, errors.New("GetBidCompanyDetail 获取数据失败")
	}
	resp := &dto.BidCompanyItem{
		Id:             data.Detail.Id,
		CompanyName:    data.Detail.CompanyName,
		OrganizingCode: data.Detail.OrganizingCode,
		Province:       data.Detail.Province,
		City:           data.Detail.City,
		AuditStatus:    data.Detail.AuditStatus,
		AuditTime:      data.Detail.AuditTime,
		AuditBy:        data.Detail.AuditBy,
		BidCount:       data.Detail.BidCount,
		CooperateCount: data.Detail.CooperateCount,
		CreatedBy:      data.Detail.CreatedBy,
		CreatedOn:      data.Detail.CreatedOn,
	}
	return resp, nil
}

func (s *strategicMonitorService) AuditBidCompany(ctx context.Context, request *dto.AuditBidCompanyRequest) (*dto.AuditBidCompanyResponse, error) {
	if request == nil {
		return nil, errors.New("AuditBidCompany 参数错误")
	}
	data, err := s.strategicMonitorRepo.AuditBidCompany(ctx, &pb.AuditBidCompanyRequest{
		Id:          request.Id,
		AuditStatus: request.AuditStatus,
		AuditUser:   authorize.GetLoginZhanTuUserName(ctx),
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("审核失败")
	}
	return &dto.AuditBidCompanyResponse{IsSuccess: data.IsSuccess}, nil
}

func (s *strategicMonitorService) AddBidCompany(ctx context.Context, request *dto.AddBidCompanyRequest) (*dto.AddBidCompanyResponse, error) {
	if request == nil || request.CompanyName == "" {
		return nil, errors.New("AddBidCompany 参数错误")
	}

	data, err := s.strategicMonitorRepo.AddBidCompany(ctx, &pb.AddBidCompanyRequest{
		CompanyName: request.CompanyName,
		Operator:    authorize.GetLoginZhanTuUserName(ctx),
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("添加失败")
	}
	return &dto.AddBidCompanyResponse{Id: data.Id}, nil
}

func (s *strategicMonitorService) GetIndustryList(ctx context.Context, request *dto.GetIndustryListRequest) (*dto.GetIndustryListResponse, error) {
	if request == nil {
		return nil, errors.New("GetIndustryList 参数错误")
	}

	data, err := s.strategicMonitorRepo.GetIndustryList(ctx, &pb.GetIndustryListRequest{
		Keyword:     request.Keyword,
		AuditStatus: request.AuditStatus,
		Page:        request.Page,
		PageSize:    request.PageSize,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("GetIndustryList 获取数据失败")
	}
	resp := &dto.GetIndustryListResponse{
		Total: data.Total,
		List:  make([]*dto.IndustryItem, 0),
	}
	for _, item := range data.List {
		resp.List = append(resp.List, &dto.IndustryItem{
			Id:              item.Id,
			IndustryName:    item.IndustryName,
			AuditStatus:     item.AuditStatus,
			BidCount:        item.BidCount,
			BidCompanyCount: item.BidCompanyCount,
			CooperateCount:  item.CooperateCount,
			CreatedBy:       item.CreatedBy,
			CreatedOn:       item.CreatedOn,
		})
	}

	return resp, nil
}

func (s *strategicMonitorService) AuditIndustry(ctx context.Context, request *dto.AuditIndustryRequest) (*dto.AuditIndustryResponse, error) {
	if request == nil {
		return nil, errors.New("AuditIndustry 参数错误")
	}
	data, err := s.strategicMonitorRepo.AuditIndustry(ctx, &pb.AuditIndustryRequest{
		Id:          request.Id,
		AuditStatus: request.AuditStatus,
		AuditUser:   authorize.GetLoginZhanTuUserName(ctx),
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("审核失败")
	}
	return &dto.AuditIndustryResponse{IsSuccess: data.IsSuccess}, nil
}

func (s *strategicMonitorService) AddIndustry(ctx context.Context, request *dto.AddIndustryRequest) (*dto.AddIndustryResponse, error) {
	if request == nil || request.IndustryName == "" {
		return nil, errors.New("AddIndustry 参数错误")
	}

	data, err := s.strategicMonitorRepo.AddIndustry(ctx, &pb.AddIndustryRequest{
		IndustryName: request.IndustryName,
		Operator:     authorize.GetLoginZhanTuUserName(ctx),
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("添加失败")
	}
	return &dto.AddIndustryResponse{Id: data.Id}, nil
}

func (s *strategicMonitorService) GetCompanyBidList(ctx context.Context, request *dto.GetCompanyBidListRequest) (*dto.GetCompanyBidListResponse, error) {
	if request == nil || request.Id == 0 {
		return nil, errors.New("GetCompanyBidList 参数错误")
	}
	data, err := s.strategicMonitorRepo.GetCompanyBidList(ctx, &pb.GetCompanyBidListRequest{
		Id:       request.Id,
		Page:     request.Page,
		PageSize: request.PageSize,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("GetCompanyBidList 获取数据失败")
	}
	resp := &dto.GetCompanyBidListResponse{
		Total: data.Total,
		List:  make([]*dto.CompanyBidItem, 0),
	}

	for _, item := range data.List {
		itemDto := &dto.CompanyBidItem{
			Id:             item.Id,
			CompanyName:    item.CompanyName,
			OrganizingCode: item.OrganizingCode,
			Title:          item.Title,
			ProcureType:    item.ProcureType,
			PublishDate:    item.PublishDate,
			StartYear:      item.StartYear,
			EndYear:        item.EndYear,
		}
		for _, industry := range item.Industries {
			itemDto.Industries = append(itemDto.Industries, &dto.IndustryItem{
				Id:              industry.Id,
				IndustryName:    industry.IndustryName,
				AuditStatus:     industry.AuditStatus,
				BidCount:        industry.BidCount,
				BidCompanyCount: industry.BidCompanyCount,
				CooperateCount:  industry.CooperateCount,
				CreatedBy:       industry.CreatedBy,
				CreatedOn:       industry.CreatedOn,
			})
		}

		resp.List = append(resp.List, itemDto)
	}
	return resp, nil
}

func (s *strategicMonitorService) GetCompanyCooperateList(ctx context.Context, request *dto.GetCompanyCooperateListRequest) (*dto.GetCompanyCooperateListResponse, error) {
	if request == nil || request.Id == 0 {
		return nil, errors.New("GetCompanyCooperateList 参数错误")
	}
	data, err := s.strategicMonitorRepo.GetCompanyCooperateList(ctx, &pb.GetCompanyCooperateListRequest{
		Id:       request.Id,
		Page:     request.Page,
		PageSize: request.PageSize,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("GetCompanyCooperateList 获取数据失败")
	}
	resp := &dto.GetCompanyCooperateListResponse{
		Total: data.Total,
		List:  make([]*dto.CompanyCooperateItem, 0),
	}

	for _, item := range data.List {
		itemDto := &dto.CompanyCooperateItem{
			Id:                      item.Id,
			CompanyName:             item.CompanyName,
			OrganizingCode:          item.OrganizingCode,
			ProcureType:             item.ProcureType,
			PublishDate:             item.PublishDate,
			StartYear:               item.StartYear,
			EndYear:                 item.EndYear,
			CooperateCompanyName:    item.CooperateCompanyName,
			CooperateOrganizingCode: item.CooperateOrganizingCode,
		}
		for _, industry := range item.Industries {
			itemDto.Industries = append(itemDto.Industries, &dto.IndustryItem{
				Id:              industry.Id,
				IndustryName:    industry.IndustryName,
				AuditStatus:     industry.AuditStatus,
				BidCount:        industry.BidCount,
				BidCompanyCount: industry.BidCompanyCount,
				CooperateCount:  industry.CooperateCount,
				CreatedBy:       industry.CreatedBy,
				CreatedOn:       industry.CreatedOn,
			})
		}

		resp.List = append(resp.List, itemDto)
	}
	return resp, nil
}

func (s *strategicMonitorService) DeleteBidCompany(ctx context.Context, request *dto.DeleteBidAuditRequest) (*dto.DeleteBidAuditResponse, error) {
	if request == nil || request.Id == 0 {
		return nil, errors.New("DeleteBidCompany 参数错误")
	}

	data, err := s.strategicMonitorRepo.DeleteBidAudit(ctx, &pb.DeleteBidAuditRequest{
		Id:       request.Id,
		Operator: authorize.GetLoginZhanTuUserName(ctx),
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("删除失败")
	}
	return &dto.DeleteBidAuditResponse{IsSuccess: data.IsSuccess}, nil
}

func (s *strategicMonitorService) DeleteCooperateCompany(ctx context.Context, request *dto.DeleteCooperateCompanyRequest) (*dto.DeleteCooperateCompanyResponse, error) {
	if request == nil || request.Id == 0 {
		return nil, errors.New("DeleteBidCompany 参数错误")
	}

	data, err := s.strategicMonitorRepo.DeleteCooperateCompany(ctx, &pb.DeleteCooperateCompanyRequest{
		Id:       request.Id,
		Operator: authorize.GetLoginZhanTuUserName(ctx),
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("删除失败")
	}
	return &dto.DeleteCooperateCompanyResponse{IsSuccess: data.IsSuccess}, nil
}

func (s *strategicMonitorService) ImportCompanyBid(ctx context.Context, request *dto.ImportCompanyBidRequest) (*dto.ImportCompanyBidResponse, error) {
	if request == nil || request.FileId == "" {
		return nil, errors.New("ImportCompanyBid 参数错误")
	}

	data, err := s.strategicMonitorRepo.ImportCompanyBid(ctx, &pb.ImportBidCompanyRequest{
		FileId:   request.FileId,
		Operator: authorize.GetLoginZhanTuUserName(ctx),
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("导入失败")
	}
	resp := &dto.ImportCompanyBidResponse{
		SucceedList: make([]string, 0),
		FailedList:  make([]string, 0),
	}

	resp.SucceedList = append(resp.SucceedList, data.SucceedList...)
	resp.FailedList = append(resp.FailedList, data.FailedList...)

	return resp, nil
}

func (s *strategicMonitorService) SubmitIndustry(ctx context.Context, request *dto.SubmitIndustryRequest) (*dto.SubmitIndustryResponse, error) {
	if request == nil || request.IndustryName == "" {
		return nil, errors.New("SubmitIndustry 参数错误")
	}

	data, err := s.strategicMonitorRepo.SubmitIndustry(ctx, &opptyPb.SubmitIndustryRequest{
		IndustryName: request.IndustryName,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("添加失败")
	}
	return &dto.SubmitIndustryResponse{Id: data.Id}, nil
}

func (s *strategicMonitorService) GetIndustryMonitorList(ctx context.Context, request *dto.GetIndustryMonitorListRequest) (*dto.GetIndustryMonitorListResponse, error) {
	data, err := s.strategicMonitorRepo.GetIndustryMonitorList(ctx, &opptyPb.GetIndustryMonitorListRequest{})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("获取数据失败")
	}
	resp := &dto.GetIndustryMonitorListResponse{
		Total: data.Total,
		List:  make([]*dto.IndustryMonitorItem, 0),
	}
	for _, item := range data.List {
		resp.List = append(resp.List, &dto.IndustryMonitorItem{
			Id:              item.Id,
			IndustryName:    item.IndustryName,
			CreatedOn:       item.CreatedOn,
			CreatedBy:       item.CreatedBy,
			BidCompanyCount: item.BidCompanyCount,
			MonitorStatus:   item.MonitorStatus,
		})
	}
	return resp, nil
}

func (s *strategicMonitorService) GetIndustrySubmitList(ctx context.Context, request *dto.GetIndustrySubmitListRequest) (*dto.GetIndustrySubmitListResponse, error) {
	data, err := s.strategicMonitorRepo.GetIndustrySubmitList(ctx, &opptyPb.GetIndustrySubmitListRequest{
		Page:     request.Page,
		PageSize: request.PageSize,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("获取数据失败")
	}
	resp := &dto.GetIndustrySubmitListResponse{
		Total: data.Total,
		List:  make([]*dto.IndustrySubmitItem, 0),
	}
	for _, item := range data.List {
		resp.List = append(resp.List, &dto.IndustrySubmitItem{
			IndustryMonitorItem: dto.IndustryMonitorItem{
				Id:              item.Id,
				IndustryName:    item.IndustryName,
				CreatedOn:       item.CreatedOn,
				CreatedBy:       item.CreatedBy,
				BidCompanyCount: item.BidCompanyCount,
				MonitorStatus:   item.MonitorStatus,
			},
			AuditStatus: item.AuditStatus,
		})
	}
	return resp, nil
}

func (s *strategicMonitorService) ChangeIndustryMonitorStatus(ctx context.Context, request *dto.ChangeIndustryMonitorStatusRequest) (*dto.ChangeIndustryMonitorStatusResponse, error) {
	data, err := s.strategicMonitorRepo.ChangeIndustryMonitorStatus(ctx, &opptyPb.ChangeIndustryMonitorStatusRequest{
		IndustryNameList: request.IndustryNameList,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("修改失败")
	}
	return &dto.ChangeIndustryMonitorStatusResponse{IsSuccess: data.IsSuccess}, nil
}

func (s *strategicMonitorService) GetIndustryStat(ctx context.Context, request *dto.GetIndustryStatRequest) (*dto.GetIndustryStatResponse, error) {
	data, err := s.strategicMonitorRepo.GetIndustryStat(ctx, &opptyPb.GetIndustryStatRequest{})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("获取数据失败")
	}
	resp := &dto.GetIndustryStatResponse{
		MonitorCount: data.MonitorCount,
		SubmitCount:  data.SubmitCount,
	}
	return resp, nil
}

func (s *strategicMonitorService) SubmitCompany(ctx context.Context, request *dto.SubmitCompanyRequest) (*dto.SubmitCompanyResponse, error) {
	if request == nil || request.CompanyName == "" {
		return nil, errors.New("SubmitCompany 参数错误")
	}

	data, err := s.strategicMonitorRepo.SubmitCompany(ctx, &opptyPb.SubmitCompanyRequest{
		CompanyName: request.CompanyName,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("添加失败")
	}
	return &dto.SubmitCompanyResponse{Id: data.Id}, nil
}

func (s *strategicMonitorService) GetCompanyMonitorList(ctx context.Context, request *dto.GetCompanyMonitorListRequest) (*dto.GetCompanyMonitorListResponse, error) {
	data, err := s.strategicMonitorRepo.GetCompanyMonitorList(ctx, &opptyPb.GetCompanyMonitorListRequest{})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("获取数据失败")
	}
	resp := &dto.GetCompanyMonitorListResponse{
		Total: data.Total,
		List:  make([]*dto.CompanyMonitorItem, 0),
	}
	for _, item := range data.List {
		resp.List = append(resp.List, &dto.CompanyMonitorItem{
			Id:             item.Id,
			CompanyName:    item.CompanyName,
			OrganizingCode: item.OrganizingCode,
			CreatedOn:      item.CreatedOn,
			CreatedBy:      item.CreatedBy,
			BidCount:       item.BidCount,
			MonitorStatus:  item.MonitorStatus,
		})
	}
	return resp, nil
}

func (s *strategicMonitorService) GetCompanySubmitList(ctx context.Context, request *dto.GetCompanySubmitListRequest) (*dto.GetCompanySubmitListResponse, error) {
	data, err := s.strategicMonitorRepo.GetCompanySubmitList(ctx, &opptyPb.GetCompanySubmitListRequest{
		Page:     request.Page,
		PageSize: request.PageSize,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("获取数据失败")
	}
	resp := &dto.GetCompanySubmitListResponse{
		Total: data.Total,
		List:  make([]*dto.CompanySubmitItem, 0),
	}
	for _, item := range data.List {
		resp.List = append(resp.List, &dto.CompanySubmitItem{
			CompanyMonitorItem: dto.CompanyMonitorItem{
				Id:             item.Id,
				CompanyName:    item.CompanyName,
				OrganizingCode: item.OrganizingCode,
				CreatedOn:      item.CreatedOn,
				CreatedBy:      item.CreatedBy,
				BidCount:       item.BidCount,
				MonitorStatus:  item.MonitorStatus,
			},
			AuditStatus: item.AuditStatus,
		})
	}
	return resp, nil
}

func (s *strategicMonitorService) ChangeCompanyMonitorStatus(ctx context.Context, request *dto.ChangeCompanyMonitorStatusRequest) (*dto.ChangeCompanyMonitorStatusResponse, error) {
	pbReq := &opptyPb.ChangeCompanyMonitorStatusRequest{}
	for _, item := range request.CompanyList {
		pbReq.List = append(pbReq.List, &opptyPb.CompanyMonitorItem{
			CompanyName:    item.CompanyName,
			OrganizingCode: item.OrganizingCode,
		})
	}
	data, err := s.strategicMonitorRepo.ChangeCompanyMonitorStatus(ctx, pbReq)
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("修改失败")
	}
	return &dto.ChangeCompanyMonitorStatusResponse{IsSuccess: data.IsSuccess}, nil
}

func (s *strategicMonitorService) GetCompanyStat(ctx context.Context, request *dto.GetCompanyStatRequest) (*dto.GetCompanyStatResponse, error) {
	data, err := s.strategicMonitorRepo.GetCompanyStat(ctx, &opptyPb.GetCompanyStatRequest{})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("获取数据失败")
	}
	resp := &dto.GetCompanyStatResponse{
		MonitorCount: data.MonitorCount,
		SubmitCount:  data.SubmitCount,
	}
	return resp, nil
}
func (s *strategicMonitorService) GetMonitorResult(ctx context.Context, request *dto.GetMonitorResultRequest) (*dto.GetMonitorResultResponse, error) {
	data, err := s.strategicMonitorRepo.GetMonitorResult(ctx, &opptyPb.GetMonitorResultRequest{
		Page: request.Page, PageSize: request.PageSize,
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, errors.New("获取数据失败")
	}
	resp := &dto.GetMonitorResultResponse{
		Total:         data.Total,
		List:          make([]*dto.MonitorResultItem, 0),
		IndustryCount: data.IndustryMonitorCount,
	}
	for _, item := range data.List {
		dtoItem := &dto.MonitorResultItem{
			CompanyName:             item.CompanyName,
			OrganizingCode:          item.OrganizingCode,
			CompanyLogo:             item.CompanyLogo,
			CompanyIdentity:         item.CompanyIdentity,
			CompanyType:             item.CompanyType,
			BidCnt2Year:             item.BidCnt_2Year,
			TakeLandCnt2Year:        item.TakeLandCnt_2Year,
			CommencePermitCnt2Year:  item.CommencePermitCnt_2Year,
			SellProjectCnt2Year:     item.SellProjectCnt_2Year,
			CooperationCaseCnt2Year: item.CooperationCaseCnt_2Year,
			ContactNum:              item.ContactNum,
			IsExistCustomer:         item.IsExistCustomer,
			Province:                item.Province,
			ProvinceCode:            item.ProvinceCode,
			City:                    item.City,
			CityCode:                item.CityCode,
			StrategicDynamic:        []*dto.StrategicDynamic{},
			CooperateCompany:        []*dto.CooperateCompany{},
		}
		if item.StrategicDynamic != nil {
			for _, item2 := range item.StrategicDynamic {
				dtoItem.StrategicDynamic = append(dtoItem.StrategicDynamic, &dto.StrategicDynamic{
					Id:                item2.Id,
					ProcureType:       item2.ProcureType,
					PublishDate:       item2.PublishDate,
					Title:             item2.Title,
					DeduceNextBidDate: item2.DeduceNextBidDate,
				})
			}
		}
		if item.CooperateCompany != nil {
			for _, item2 := range item.CooperateCompany {
				industryRemark := ""
				if len(item2.IndustryNames) != 0 {
					industryRemark = strings.Join(item2.IndustryNames, ",") + "品类合作"
				}
				dtoItem.CooperateCompany = append(dtoItem.CooperateCompany, &dto.CooperateCompany{
					CooperateCompanyName: item2.CooperateCompanyName,
					PublishDate:          item2.PublishDate,
					IndustryRemark:       industryRemark,
				})
			}
		}
		resp.List = append(resp.List, dtoItem)
	}
	return resp, nil
}
